package com.bluesky.plum.uimodels.render.html.components.containers.window;

import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.javawebbrowser.domain.html.tags.form.Form;
import com.bluesky.javawebbrowser.domain.html.tags.form.HttpMethod;
import com.bluesky.javawebbrowser.domain.html.tags.form.input.Hidden;
import com.bluesky.javawebbrowser.domain.html.tags.layout.DIV;
import com.bluesky.plum.uimodels.render.html.PMEServlet;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.window.MainWindow;
import com.bluesky.plum.uimodels.standard.components.containers.window.Window;
import com.bluesky.plum.uimodels.standard.components.menu.MenuBar;

public class HMainWindow extends MainWindow {

	/**
	 * the native HTML tags
	 */
	// Head Tag
	// protected Body bodyTag;
	protected DIV mainWrapper;
	protected DIV menu;
	protected DIV content;
	protected Form formTag;

	protected HWindowThread eventHandlerThread;

	public HMainWindow() {
		super();

		mainWrapper = new DIV();
		mainWrapper.setAttribute("class", "main");
		mainWrapper.setStyleAttribute("clear", "both");

		menu = new DIV();
		menu.setAttribute("class", "menu");
		menu.setStyleAttribute("clear", "both");

		content = new DIV();
		content.setAttribute("class", "content");
		content.setStyleAttribute("clear", "both");

		formTag = new Form();
		formTag = new com.bluesky.javawebbrowser.domain.html.tags.form.Form();
		formTag.setAction("processEvent");
		formTag.setMethod(HttpMethod.Post);

		// event_name
		Hidden oumEventName = new Hidden();
		oumEventName.setName(PMEServlet.OUM_EVENT_NAME);
		oumEventName.setValue("");// Btn1.onClick()

		Hidden oumComponentId = new Hidden();
		oumComponentId.setName(PMEServlet.OUM_COMPONENT_ID);
		oumComponentId.setValue("");// Btn1.onClick()

		formTag.addChild(oumEventName);
		formTag.addChild(oumComponentId);

		formTag.addChild(content);

		mainWrapper.addChild(menu);
		mainWrapper.addChild(formTag);

		eventHandlerThread = new HWindowThread(this);
		eventHandlerThread.start();
	}

	@Override
	public void setTitle(String title) {
		super.setTitle(title);

		//for debug sake
		eventHandlerThread.refreshName();
		
		// head.setTitle(title);
	}

	@Override
	public void setMenuBar(MenuBar menuBar) {
		super.setMenuBar(menuBar);

		menu.addChild((Tag) menuBar.getNativeComponent());
	}

	public DIV getMain() {
		return mainWrapper;
	}

	public void setMain(DIV main) {
		this.mainWrapper = main;
	}

	public DIV getMenu() {
		return menu;
	}

	public void setMenu(DIV menu) {
		this.menu = menu;
	}

	@Override
	public void setContentPanel(Panel contentPanel) {

		super.setContentPanel(contentPanel);

		// synchronize the native model
		content.removeAllChildren();
		content.addChild((Tag) contentPanel.getNativeComponent());
		// content = (Tag)contentPanel.getNativeComponent();
	}

	/**
	 * get the HTML Tag Model, a.k.a. DOM
	 */
	@Override
	public Object getNativeComponent() {

		return mainWrapper;
	}

	@Override
	public Thread getEventHandlerThread() {
		return eventHandlerThread;
	}

	public void setEventHandlerThread(HWindowThread eventHandlerThread) {
		this.eventHandlerThread = eventHandlerThread;
	}

	/*
	 * it is different from the ((Tag)getNativeComponent).toHtml(); it will
	 * decide how to display the main window and child window, eg. a child could
	 * be in pop up style or MDI child window style.
	 */
	public String render() {

		/*
		 * just temporarily swap the font window and the content panel
		 */

		Panel oldCcontentPanel = getContentPanel();

		Window fontWindow = getWindowManager().getVisibleFontWindow();
		getWindowManager().print();
		String s;
		if ( fontWindow != this) {
			System.out.println("There is a dialog window (or more).");

			
			Panel newPanel = (Panel) getUiComponentBuilder()
					.createNonFieldComponent(StandardUIComponentType.Panel);
			newPanel.addChildComponent(fontWindow);
			setContentPanel(newPanel);
			
			menu.setAttribute("disabled", "disabled");

			s = ((Tag) getNativeComponent()).toHtml();

			// restore
			setContentPanel(oldCcontentPanel);
			menu.removeAttribute("disabled");
			
			//s = ((Tag)getWindowManager().getFontWindow().getNativeComponent()).toHtml();
		} else {
			System.out.println("There is no dialog window.");
			s = ((Tag) getNativeComponent()).toHtml();
		}
		return s;
	}
	

}
